/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package example;
import facecat.topin.core.*;
import facecat.topin.btn.*;

/*
* 透明按钮
*/
public class MyButton extends FCButton {
    /*
    * 创建按钮
    */
    public MyButton()
    {
        setBorderColor(FCColor.None);
        setTextColor(FCColor.rgb(255, 255, 255));
        setFont(new FCFont("Default", 14, true, false, false));
        setBackColor(FCColor.rgb(150, 150, 150));
        setCornerRadius(4);
    }

    /*
    * 销毁对象
    */
    public void delete()
    {
        stopTimer(m_timerID);
        super.delete();
    }

    /*
    * 移除视图
    */
    public void onRemove()
    {
        super.onRemove();
        stopTimer(m_timerID);
    }

    /*
    * 秒表ID
    */
    private int m_timerID = FCView.getNewTimerID();

    /*
    * 计数器
    */
    private int m_tick = 0;

    /*
    * 触摸移入事件
    * @param touchInfo 触摸信息
    */
    public void onTouchEnter(FCTouchInfo touchInfo)
    {
        super.onTouchEnter(touchInfo);
        m_tick = 60;
        startTimer(m_timerID, 10);
    }

    /*
     * 秒表事件
     */
    public void onTimer(int timerID)
    {
        super.onTimer(timerID);
        if (m_tick > 0)
        {
            m_tick--;
            if (m_tick == 0)
            {
                stopTimer(m_timerID);
            }
            invalidate();
        }
    }

    /*
    * 重绘方法
    * @param paint 绘图对象
    * @param clipRect 裁剪区域
    */
    public void onPaint(FCPaint paint, FCRect clipRect)
    {
        String text = getText();
        //绘制文字
        if (text != null && text.length() >= 0)
        {
            int width = getWidth(), height = getHeight();
            if (width > 0 && height > 0)
            {
                FCRect drawRect = new FCRect(1, 1, width - 1, height - 1);
                if (m_tick > 0 || this == m_native.getHoveredView())
                {
                    paint.fillRoundRect(FCColor.rgba(42, 138, 195, 135 + 120 * (60 - m_tick) / 60), drawRect, getCornerRadius());
                }
                else
                {
                    paint.fillRoundRect(getPaintingBackColor(), drawRect, getCornerRadius());
                }

                FCFont font = getFont();
                FCSize tSize = paint.textSize(text, font, -1);
                FCRect tRect = new FCRect((width - tSize.cx) / 2, (height - tSize.cy) / 2, (width - tSize.cx) / 2, (height + tSize.cy) / 2);
                paint.drawText(text, getPaintingTextColor(), font, tRect);
            }
        }
    }
}
